package boot.spring.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import boot.spring.po.Actor;
import boot.spring.po.AjaxResult;
import boot.spring.util.SerializationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Api(tags = "redis接口")
@RestController
@RequestMapping("/redis")
public class RedisController {
	
	@Autowired
	JedisPool pool;
	
	@ApiOperation("获取字符串类型的值")
	@RequestMapping(value="/string",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult getString(@RequestParam String key) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		String value = jedis.get(key);
		jedis.close();
		return AjaxResult.success(value);
	}
	
	@ApiOperation("写入字符串类型的值")
	@RequestMapping(value="/string",method = RequestMethod.PUT)
	@ResponseBody
	public  AjaxResult setString(@RequestParam String key, @RequestParam String value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		String result = jedis.set(key, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("读取hashmap的值")
	@RequestMapping(value="/getmap",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult getMap(@RequestParam String key) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Map<String, String> map = jedis.hgetAll(key);
		jedis.close();
		return AjaxResult.success(map);
	}
	
	@ApiOperation("向hashmap添加值")
	@RequestMapping(value="/putmap",method = RequestMethod.POST)
	@ResponseBody
	public  AjaxResult getMap(@RequestParam String key, @RequestParam String field, @RequestParam String value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.hset(key, field, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("向hashmap删除值")
	@RequestMapping(value="/delmap",method = RequestMethod.DELETE)
	@ResponseBody
	public  AjaxResult delMap(@RequestParam String key, @RequestParam String field) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.hdel(key, field);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("读取list的值")
	@RequestMapping(value="/getList",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult getList(@RequestParam String key) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		List<String> list = jedis.lrange(key, 0, -1);
		jedis.close();
		return AjaxResult.success(list);
	}
	
	@ApiOperation("给list添加元素")
	@RequestMapping(value="/addList",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult addList(@RequestParam String key, @RequestParam String[] value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.rpush(key, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("移除list指定值的元素")
	@RequestMapping(value="/removeList",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult removeList(@RequestParam String key, @RequestParam String value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.lrem(key, 0, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("读取set的值")
	@RequestMapping(value="/getSet",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult getSet(@RequestParam String key) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Set<String> set = jedis.smembers(key);
		jedis.close();
		return AjaxResult.success(set);
	}	
	
	@ApiOperation("给set添加元素")
	@RequestMapping(value="/putSet",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult putSet(@RequestParam String key, @RequestParam String[] value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.sadd(key, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("给set删除元素")
	@RequestMapping(value="/delSet",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult delSet(@RequestParam String key, @RequestParam String[] value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.srem(key, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("读取有序set的值")
	@RequestMapping(value="/getSortedSet",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult getSortedSet(@RequestParam String key) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Set<String> set = jedis.zrange(key, 0, -1);
		jedis.close();
		return AjaxResult.success(set);
	}
	
	@ApiOperation("删除有序set的值")
	@RequestMapping(value="/delSortedSet",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult delSortedSet(@RequestParam String key, @RequestParam String value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.zrem(key, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("添加有序set的值")
	@RequestMapping(value="/addSortedSet",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult addSortedSet(@RequestParam String key,@RequestParam Double score, @RequestParam String value) {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		Long result = jedis.zadd(key, score, value);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("用string类型写入对象")
	@RequestMapping(value="/addObject",method = RequestMethod.POST)
	@ResponseBody
	public  AjaxResult addObject(@RequestParam String key, @RequestBody Actor actor) throws Exception {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		byte[] bytes = SerializationUtils.serialize(actor);
		String result = jedis.set(key.getBytes(), bytes);
		jedis.close();
		return AjaxResult.success(result);
	}
	
	@ApiOperation("读取string类型中的对象")
	@RequestMapping(value="/getObject",method = RequestMethod.GET)
	@ResponseBody
	public  AjaxResult getObject(@RequestParam String key) throws Exception {
		Jedis jedis = pool.getResource();
		jedis.select(0);
		byte[] actor = jedis.get(key.getBytes());
	    Actor r = SerializationUtils.deserialize(actor, Actor.class);
		jedis.close();
		return AjaxResult.success(r);
	}
	
}